<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="no.ntnu.tdt4237.*"%>
<%@ page import="no.ntnu.tdt4237.helperactions.*"%>
<%@page import="nl.captcha.Captcha"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@page import="java.util.Date"%><html
	xmlns="http://www.w3.org/1999/xhtml">

<tags:header title=" - Post"></tags:header>

<body>

	<%
User loggedInUser = (User) session.getAttribute(SessionKeys.USER_OBJECT);
String blogOwnerName = StringHelpers.getPostOwnerName(request.getRequestURL().toString());
User postOwner = Database.getUser(blogOwnerName);

if (loggedInUser != null){
	%>	<tags:menu loggedInUser="<%=loggedInUser%>"></tags:menu> <%
}else{
	%> <tags:menu></tags:menu> <%
}

if (postOwner!=null) 
{
	String postDate = StringHelpers.getPostDate(request.getRequestURL().toString(), postOwner.getUserName());
	String postTitle = StringHelpers.getPostTitle(request.getRequestURL().toString(), postOwner.getUserName());
	BlogPost post = Database.getPost(postOwner, postTitle, DateHelpers.parseString(postDate));
	
	if (post!=null) 
	{
		
		if (request.getParameter("text") != null && request.getParameter("answer") != null){
			Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
			String answer = request.getParameter("answer");
			if (captcha.isCorrect(answer)){
			
				if (request.getParameter("nickname") != null && request.getParameter("email") != null){
					if (Database.userNotExist("nickname") && !Database.emailIsUsed(request.getParameter("email"))){
					Database.saveComment( new Comment((String) request.getParameter("nickname"), (String) request.getParameter("text"), new Date(), post));
					}else{
						%>
						<tags:message message="Your nickname or email belongs to a registered user on the blog! So now you know, Trond."></tags:message>
						<%
					}
				}else if (session.getAttribute(SessionKeys.USER_OBJECT) != null){
				Database.saveComment(new Comment(loggedInUser, (String) request.getParameter("text"), new Date(), post));
				}
				
			}else{
				%> <tags:message message="Wrong captcha! Try again."></tags:message> <%
			}
			
		}
				
		if (loggedInUser != null){
			
			if (loggedInUser.getUserName().equals(post.getOwner().getUserName())){ //Logget inn og eier posten.
				
				if (request.getParameter("Delete") != null){
					Database.deletePost(post);
					%> <tags:message message="The post is deleted."></tags:message> <%
							
				}else if (request.getParameter("DeleteComment") != null){
					User userOfComment = Database.getUser(request.getParameter("dOwner"));
					Comment comment = new Comment(userOfComment, (String) request.getParameter("dText"), DateHelpers.parseString(request.getParameter("dDate")), post);		
					Database.deleteComment(comment);
					%> <tags:message message="The comment is deleted."></tags:message> <%
				}
				
				%> <tags:blogpost post="<%=post %>" loggedInUser="<%=loggedInUser %>" 	full="true"> </tags:blogpost> <%
						
				for(Comment com : Database.getPostComments(post))
					{
						%> <tags:comment comment="<%=com%>" showDelete="True"></tags:comment> <%
					}
						
				%> <tags:addcomment></tags:addcomment> <%
			
			}else{ //Else til if(loggedInUser.getUserName().equals(post.getOwner().getUserName)). Er logget inn, men eier ikke posten.
				%> <tags:blogpost post="<%=post %>" loggedInUser="<%=loggedInUser %>" 	full="true"> </tags:blogpost> <%
				
				for(Comment com : Database.getPostComments(post))
					{
						%> <tags:comment comment="<%=com%>" showDelete="False"></tags:comment> <%
					}
				
				%> <tags:addcomment></tags:addcomment> <%
			}
			
			
		}else{ //else til if(loggedInUser != null) bare ser på bloggen, ikke logget inn.
			
			%> <tags:blogpost post="<%=post %>" full="true"> </tags:blogpost> <%
			for(Comment com : Database.getPostComments(post))
				{
					%> <tags:comment comment="<%=com%>" showDelete="False"></tags:comment> <%
				}
			
			
			%>
		<tags:addcommentanon></tags:addcommentanon>
			<% 
		}
		
	}else{ //else til if(post!=null) Fant ikke posten.
		%> <tags:error errorMsg="This post doesn't exist"></tags:error> <%
	}
}else{ //Fant ingen som eier posten eller brukeren er på post uten noe i requestURL.

	%> <tags:error errorMsg="The post you are looking for does not exist. Try browsing our registered user's blogs in the menu to the left."></tags:error> <%

}


%>
</body>
</html>